<?php
class CustomerController extends BackendController
{

    public $active = "plugins-customer";
    public $plug_version = "1.0";
    public $plug_title = "首页客户反馈";
    public $plug_description = "首页客户反馈";
    public $plug_controller = "customer";


    public function actionIndex(){

        $action_type = isset($_REQUEST['action_type'])?$_REQUEST['action_type']:null;

        if($action_type == 'insert' || $action_type =='updatedo'){
            $plug_customer_id = $_REQUEST['plug_customer_id'];
            $content = $_REQUEST['content'];
            $image_address = $_REQUEST['image_address'];
            $title = $_REQUEST['title'];
            $position = $_REQUEST['position'];
            $seq = $_REQUEST['seq'];

            if($plug_customer_id == 0){
                $this->connection->createCommand("insert into rk_plug_customer (image_address, content, title, position,seq) values
 (:image_address, :content, :title, :position,:seq)")
                    ->bindParam(":content",$content)
                    ->bindParam(":image_address",$image_address)
                    ->bindParam(":title",$title)
                    ->bindParam(":position",$position)
                    ->bindParam(":seq",$seq)
                    ->execute();
            }else{
                $this->connection->createCommand("update rk_plug_customer set image_address = :image_address,title =:title,content=:content,seq=:seq,position=:position 
where plug_customer_id = :plug_customer_id ")
                    ->bindParam(":content",$content)
                    ->bindParam(":image_address",$image_address)
                    ->bindParam(":title",$title)
                    ->bindParam(":position",$position)
                    ->bindParam(":seq",$seq)
                    ->bindParam("plug_customer_id",$plug_customer_id)
                    ->execute();
            }

        }else if($action_type == 'update'){
            $plug_customer_id = $_REQUEST['plug_customer_id'];
            $this->data['update_customer'] = $this->connection->createCommand("select * from rk_plug_customer where plug_customer_id = :plug_customer_id ")
                ->bindParam("plug_customer_id",$plug_customer_id)
                ->queryRow();
        }else if($action_type == 'delete'){
            $plug_customer_id = $_REQUEST['plug_customer_id'];
            $this->connection->createCommand("delete from rk_plug_customer where plug_customer_id = :plug_customer_id ")
                ->bindParam("plug_customer_id",$plug_customer_id)
                ->execute();
        }

        $this->data['customers'] = $this->connection->createCommand("select * from rk_plug_customer order by seq desc")->queryAll();

        $this->render("index",$this->data);
    }

    public function actionInstall(){

        $this->connection->createCommand("DROP TABLE IF EXISTS `rk_plug_customer`")->execute();
        $this->connection->createCommand("CREATE TABLE `rk_plug_customer` (
  `plug_customer_id` int(11) NOT NULL AUTO_INCREMENT,
  `image_address` varchar(255) DEFAULT NULL,
  `content` text,
  `title` varchar(255) DEFAULT NULL,
  `position` varchar(255) DEFAULT NULL,
  `seq` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`plug_customer_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8")->execute();
        $this->connection->createCommand("insert into rk_plug (plug_controller,plug_version,plug_key) values (:controller,:version,:key)")
            ->bindParam(":controller",$this->plug_controller)
            ->bindParam(":version",$this->plug_version)
            ->bindParam(":key",$this->active)
            ->execute();
        $parentMenuId = $this->connection->createCommand("SELECT menu_id FROM rk_menu WHERE menu_key = 'plugins'")->queryScalar();
        $this->connection->createCommand("INSERT INTO rk_menu ( menu_text, parent_id, url, menu_key ) VALUES (:menu_text,:parent_id,'index.php?r=plugins/customer',:menu_key)")
            ->bindParam(":menu_text",$this->plug_title)
            ->bindParam(":parent_id",$parentMenuId)
            ->bindParam(":menu_key",$this->active)
            ->execute();
        echo json_encode(AppCode::$success);
    }

    public function actionUninstall(){
        $this->connection->createCommand("delete from rk_plug where plug_key = :plug_key")
            ->bindParam(":plug_key",$this->active)
            ->execute();
        $this->connection->createCommand("DROP TABLE IF EXISTS `rk_plug_service`")->execute();
        $this->connection->createCommand("delete from rk_menu where menu_key = :menu_key")
            ->bindParam(":menu_key",$this->active)
            ->execute();
        echo json_encode(AppCode::$success);
    }


}